from .node import *
from .core import *
from .hosting import *
from .origin import app
from .core_user import *
from .core_info import *
from .core_role import *

# from .web_server import *
from .system_info import *
from src.core.enums import Enums
from src.core.variables.constant import CONF


def debug_join():
    """调试用路由，生产环境下禁止开启"""

    def clear_db_hosting_java():
        """清除 hosting_Java 数据库"""
        from src.debug.clear.db.hosting_Java import App

        app.add_url_rule(
            "/api/debug/clear/db/hosting_Java/App",
            view_func=App,
            methods=["POST", "GET"],
        )

    clear_db_hosting_java()


# def web_server_join():
#     """web 服务器路由"""
#     # Web 模块路由 ================================================
#     app.add_url_rule("/", view_func=catch_all, methods=["GET"])
#     app.add_url_rule("/<path:path>", view_func=catch_all, methods=["GET"])
#     app.add_url_rule("/<path:filename>", view_func=static_files, methods=["GET"])


def core_join():
    def user():
        """用户模块路由"""
        app.add_url_rule(
            "/api/core/user/login", view_func=login, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/logout", view_func=logout, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/signup", view_func=signup, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/remove_user", view_func=remove_user, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/update_user", view_func=update_user, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/user_list", view_func=user_list, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/user_info", view_func=user_info, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/refresh_token",
            view_func=refresh_token,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/core/user/change_passwd",
            view_func=change_passwd,
            methods=["POST", "GET"],
        )

    def role():
        """角色模块路由"""
        app.add_url_rule(
            "/api/core/user/add_role", view_func=add_role, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/roles_list", view_func=roles_list, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/remove_role", view_func=remove_role, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/update_role", view_func=update_role, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/user/role_info", view_func=role_info, methods=["POST", "GET"]
        )

    def node():
        """多节点路由"""
        app.add_url_rule(
            "/api/core/node/bind_new", view_func=nodes_bind, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/node/nodes_list", view_func=nodes_list, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/node/bind_info", view_func=bind_info, methods=["POST", "GET"]
        )
        app.add_url_rule(
            "/api/core/node/unbind_node", view_func=unbind_node, methods=["POST", "GET"]
        )

    app.add_url_rule(
        "/api/core/close_sse", view_func=close_sse, methods=["POST", "GET"]
    )
    app.add_url_rule(
        "/api/core/info/all_nodes", view_func=all_nodes, methods=["POST", "GET"]
    )
    app.add_url_rule(
        "/api/core/user/verify_token", view_func=check_token, methods=["POST", "GET"]
    )
    node()
    user()
    role()


def hosting_join():
    """托管模块路由"""

    def manager():
        """管理路由"""
        app.add_url_rule(
            "/api/apps/hosting/register",
            view_func=register,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/deploy",
            view_func=deploy,
            methods=["POST", "GET"],
        )
        # app.add_url_rule(
        #     "/api/apps/hosting/config",
        #     view_func=config,
        #     methods=["POST", "GET"],
        # )
        app.add_url_rule(
            "/api/apps/hosting/remove",
            view_func=remove,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/control",
            view_func=control,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/info",
            view_func=info,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/rollback",
            view_func=rollback,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/rollback/remove",
            view_func=remove_record,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/file_editor/look",
            view_func=look_file_content,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/file_editor/change",
            view_func=file_change,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/static/ngx_conf",
            view_func=fm_nginx_template_conf,
            methods=["POST", "GET"],
        )

    def other():
        """其他路由"""
        app.add_url_rule(
            "/api/apps/hosting/receive_file",
            view_func=receive_file,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/realog",
            view_func=realog,
            methods=["POST", "GET"],
        )

        # app.add_url_rule(
        #     "/api/apps/hosting/stop_realog",
        #     view_func=stop_realog,
        #     methods=["POST", "GET"],
        # )
        app.add_url_rule(
            "/api/apps/hosting/all_apps",
            view_func=all_apps,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/all_stypes",
            view_func=all_stypes,
            methods=["POST", "GET"],
        )
        # app.add_url_rule(
        #     "/api/apps/hosting/all_apps_status",
        #     view_func=all_apps_status,
        #     methods=["POST", "GET"],
        # )
        app.add_url_rule(
            "/api/apps/hosting/migrate",
            view_func=migrate,
            methods=["POST", "GET"],
        )
        # app.add_url_rule(
        #     "/api/apps/hosting/histlog_list",
        #     view_func=histlog_list,
        #     methods=["POST", "GET"],
        # )
        # app.add_url_rule(
        #     "/api/apps/hosting/histlog",
        #     view_func=histlog,
        #     methods=["POST", "GET"],
        # )
        app.add_url_rule(
            "/api/apps/hosting/download_file",
            view_func=download_file,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/count",
            view_func=count,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/apps/hosting/check_choose_file",
            view_func=check_choose_file,
            methods=["POST", "GET"],
        )

    manager()
    other()


def system_join():
    """系统模块路由"""

    def info_join():
        """系统信息模块路由"""
        # app.add_url_rule(
        #     "/api/apps/system/info/realtime",
        #     view_func=realtime,
        #     methods=["POST", "GET"],
        # )
        # app.add_url_rule(
        #     "/api/apps/system/info/system",
        #     view_func=system_info,
        #     methods=["POST", "GET"],
        # )
        # app.add_url_rule(
        #     "/api/apps/system/info/fm_occ_space",
        #     view_func=fm_occ_space,
        #     methods=["POST", "GET"],
        # )
        app.add_url_rule(
            "/api/apps/system/info/reaload",
            view_func=reaload,
            methods=["POST", "GET"],
        )
        app.add_url_rule(
            "/api/core/system/wfs",
            view_func=wfs,
            methods=["POST", "GET"],
        )
        # 新增的合并基础信息接口
        app.add_url_rule(
            "/api/core/nodes/basic_info",
            view_func=basic_info,
            methods=["POST", "GET"],
        )

    info_join()


def join():
    if CONF.LOG_LEVEL is Enums.LogLevel.DEBUG:
        debug_join()
    # 核心路由
    core_join()
    # 托管路由
    hosting_join()
    # 系统模块路由
    system_join()
    # web服务器路由
    # web_server_join()
    return app
